package cn.cloud9.api;

import java.io.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class API07Input {

    final static String SAMPLE = "src\\cn\\cloud9\\api\\API07Input.java";

    public static void main(String[] args) {
        // 根据user.dir进行读取的
        // System.out.println(System.getProperty("user.dir"));
        // final String read = read(SAMPLE);
        // System.out.println(read);

        // 输出
        // write(SAMPLE, "API07Input.dat");

        // 内存读取？
        // memoryRead(SAMPLE);


        // customWriteFile("dd.txt");
        read2(SAMPLE);
    }

    /**
     * 文件读取，转换成字符串
     * @param filename
     * @return
     */
    public static String read(String filename) {
        try (BufferedReader reader = new BufferedReader( new FileReader(filename))) {
            final Stream<String> lines = reader.lines();
            final String collect = lines.collect(Collectors.joining("\n"));
            return collect;
        } catch (Exception exception) {
            String message = exception.getMessage();
            exception.printStackTrace();
        }
        return null;
    }

    /**
     * 逐码读取
     * @param filename
     */
    public static void memoryRead(String filename) {
        try (
            StringReader reader = new StringReader( read(filename));
        ) {
            int c;
            while ( (c = reader.read()) != -1) {
                System.out.print((char)c);
            }


        } catch (Exception exception) {
            String message = exception.getMessage();
            exception.printStackTrace();
        }
    }

    /**
     * 自定义输出文件
     * @param filename
     */
    public static void customWriteFile(String filename) {
        try (
            DataOutputStream outStream =  new DataOutputStream( new BufferedOutputStream( new FileOutputStream(filename)))
        ) {
            outStream.flush();
            outStream.writeUTF("ABCD !$%@ 一二三四 《》？\n");
            outStream.writeUTF("12 3 ABCD !$%@ 一二三四 《》？\n");

        } catch (Exception exception) {
            String message = exception.getMessage();
            exception.printStackTrace();
        }
    }

    public static void read2(String filename) {
        try (final DataInputStream inputStream = new DataInputStream(new FileInputStream(filename))) {

            while (inputStream.read() != -1) {
                final String s = inputStream.readUTF();
                System.out.println(s);
            }
        } catch (Exception exception) {
            String message = exception.getMessage();
            exception.printStackTrace();
        }
    }

    /**
     * 输出文件
     * @param readFile
     * @param writeFile
     */
    public static void write(String readFile, String writeFile) {
        try (
                // 输入流
                final BufferedReader reader = new BufferedReader( new StringReader( read(readFile)));
                // 输出流
                final PrintWriter writer = new PrintWriter( new BufferedWriter( new FileWriter(writeFile)));
                // 输入流写流对象给输出流输出

                ) {
            reader.lines().forEach(writer::println);
        } catch (Exception exception) {
            final String message = exception.getMessage();
            exception.printStackTrace();
        }

    }




}
